<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\Employee;
use App\Models\Post;
use App\Models\PostType;
use App\Models\PersonnelDynamics;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class PostTypeController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];

        if ( $request->post_type_id) {
            $post_type = PostType::findOrFail($request->post_type_id);
            $post_type->mtime = time();
        } else {
            $post_type = new PostType();
            $post_type->ctime = time();
        }
        $post_type->post_type_name = $request->post_type_name;
        $post_type->company_id = $company_id;
        $post_type->post_type_code = $request->post_type_code;
        $post_type->post_type_description = $request->post_type_description;
        $post_type->uid = $uid;

        $post_type->save();

        return showError(Error::ERR_OK,['post_type_id'=>$post_type->post_type_id]);

    }

    public function show(Request $request) {
        $post_type = PostType::findOrFail($request->post_type_id);
        return showError(Error::ERR_OK,['post_type'=>$post_type]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');
        if (!empty($name))
        {
            $where['name'] = $name;
        }
        $code = $request->input('code');
        if (!empty($code))
        {
            $where['code'] = $code;
        }
//        $status = $request->input('status');
//        if ($status > 0)
//        {
        $where['company_id'] = $company_id;
        $where['status'] = 1;
//        }

        $limit = $request->limit?:'20';

        $list = PostType::getList($where,$limit);
        if ($limit == 20) {
            foreach ($list as $k => &$v) {
                $v->post_num = Post::where('company_id',$company_id)->where('post_type_id',$v->post_type_id)->where('status',1)->count();
                $posts = Post::where('company_id',$company_id)->where('post_type_id',$v->post_type_id)->where('status',1)->get();
                $post_ids = [];
                foreach ($posts as $ke=>$va) {
                    $post_ids[] = $va['post_id'];
                }
                $v->employee_num = Employee::whereIn('post_id',$post_ids)->where('company_id',$company_id)->where('status',1)->count();
            }
        }

        return showError(Error::ERR_OK,['list'=>$list]);
    }

    public function delete(Request $request) {
        PostType::where('post_type_id',$request->post_type_id)->update(['status'=>2,'mtime'=>time()]);
        return showError(Error::ERR_OK);
    }

}
